package com.dongdongshop.controller;

import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.CategoryService;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    @DubboReference
    private ContentService cs;

    @DubboReference
    private CategoryService gs;

    @RequestMapping("show")
    public String show(Model m){
        m.addAttribute("contentCatList",gs.getAll(null));
        return "admin/content";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public Result getAll(String name){
        try {
            List<TbContent> all = cs.getAll();
            return Result.OK().setData(all);
        } catch (Exception e) {
            return Result.ERROR();
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbContent t){
        try {
            cs.insert(t);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result getById(Long id,Model m){
        try {
            TbContent byId = cs.getById(id);
            m.addAttribute("c",byId);
            return Result.OK().setData(byId);
        } catch (Exception e) {
            return Result.response(ResultEnum.GETBYID_ERROR);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbContent t){
        try {
            cs.update(t);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("deleteCheck")
    @ResponseBody
    public Result deleteCheck(Long[] ids){
        try {
            cs.deleteCheck(ids);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }

}
